package com.x.config;

/**
 * @title: DynamicContextHolder
 * @Author X
 * @Date: 2021/1/4 14:59
 * @Version 1.0
 */
public class DynamicDataSourceContextHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return "defaultDataSource";
        }
    };

    /**
     * 设置数据源
     * @param dataSourceId
     */
    public static void setDataSource(String dataSourceId) {
        contextHolder.set(dataSourceId);
    }

    /**
     * 取得当前数据源
     * @return
     */
    public static String getDataSource() {
        return contextHolder.get();
    }

    /**
     * 清除上下文数据
     */
    public static void clearDataSource() { contextHolder.remove(); }

}
